﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PlantHire.Controllers
{
    public class PlantMachineController : Controller
    {
        private PlantHireEntities db = new PlantHireEntities();

        //
        // GET: /PlantMachine/

        public ActionResult Index()
        {
            var plantmachines = db.PlantMachines.Include(p => p.Company).Include(p => p.Location).Include(p => p.SizeModel);
            return View(plantmachines.ToList());
        }

        //
        // GET: /PlantMachine/Details/5

        public ActionResult Details(int id = 0)
        {
            PlantMachine plantmachine = db.PlantMachines.Find(id);
            if (plantmachine == null)
            {
                return HttpNotFound();
            }
            return View(plantmachine);
        }

        //
        // GET: /PlantMachine/Create

        public ActionResult Create()
        {
            ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName");
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Value");
            ViewBag.SizeModelId = new SelectList(db.SizeModels, "SizeModelId", "Value");
            return View();
        }

        //
        // POST: /PlantMachine/Create

        [HttpPost]
        public ActionResult Create(PlantMachine plantmachine)
        {
            if (ModelState.IsValid)
            {
                db.PlantMachines.Add(plantmachine);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName", plantmachine.CompanyId);
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Value", plantmachine.LocationId);
            ViewBag.SizeModelId = new SelectList(db.SizeModels, "SizeModelId", "Value", plantmachine.SizeModelId);
            return View(plantmachine);
        }

        //
        // GET: /PlantMachine/Edit/5

        public ActionResult Edit(int id = 0)
        {
            PlantMachine plantmachine = db.PlantMachines.Find(id);
            if (plantmachine == null)
            {
                return HttpNotFound();
            }
            ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName", plantmachine.CompanyId);
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Value", plantmachine.LocationId);
            ViewBag.SizeModelId = new SelectList(db.SizeModels, "SizeModelId", "Value", plantmachine.SizeModelId);
            return View(plantmachine);
        }

        //
        // POST: /PlantMachine/Edit/5

        [HttpPost]
        public ActionResult Edit(PlantMachine plantmachine)
        {
            if (ModelState.IsValid)
            {
                db.Entry(plantmachine).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName", plantmachine.CompanyId);
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Value", plantmachine.LocationId);
            ViewBag.SizeModelId = new SelectList(db.SizeModels, "SizeModelId", "Value", plantmachine.SizeModelId);
            return View(plantmachine);
        }

        //
        // GET: /PlantMachine/Delete/5

        public ActionResult Delete(int id = 0)
        {
            PlantMachine plantmachine = db.PlantMachines.Find(id);
            if (plantmachine == null)
            {
                return HttpNotFound();
            }
            return View(plantmachine);
        }

        //
        // POST: /PlantMachine/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            PlantMachine plantmachine = db.PlantMachines.Find(id);
            db.PlantMachines.Remove(plantmachine);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}